package com.qn.visitor.analyze.filter;


import com.qn.visitor.analyze.filter.strategy.IAnalyzeFilter;
import com.qn.visitor.analyze.filter.strategy.guava.ClientLanguageGuavaFilter;
import com.qn.visitor.analyze.filter.strategy.guava.IpAddressrGuavaFilter;
import com.qn.visitor.analyze.filter.strategy.guava.UserAgentGuavaFilter;
import com.qn.visitor.analyze.filter.strategy.redis.ClientLanguageRedisFilter;
import com.qn.visitor.analyze.filter.strategy.redis.IpAddressrRedisFilter;
import com.qn.visitor.analyze.filter.strategy.redis.UserAgentRedisFilter;
import com.qn.visitor.resources.config.FilterTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@Component
public class FilterContext {
    @Autowired
    FilterProperties properties;
    @Autowired
    ApplicationContext applicationContext;
    public IAnalyzeFilter clInstance(){
        if (properties.getFilterType() == null) {
            return null;
        }
        IAnalyzeFilter strategy;
        if (properties.getFilterType().equals(FilterTypeUtils.REDIS)) {
            strategy = applicationContext.getBean(ClientLanguageRedisFilter.class);
        } else if (properties.getFilterType().equals(FilterTypeUtils.GUAVA)) {
            strategy = ClientLanguageGuavaFilter.getInstance();
        } else {
            strategy = ClientLanguageGuavaFilter.getInstance();
        }
        return strategy;
    }
    public IAnalyzeFilter ipInstance(){
        if (properties.getFilterType() == null) {
            return null;
        }
        IAnalyzeFilter strategy;
        if (properties.getFilterType().equals(FilterTypeUtils.REDIS)) {
            strategy = applicationContext.getBean(IpAddressrRedisFilter.class);
        } else if (properties.getFilterType().equals(FilterTypeUtils.GUAVA)) {
            strategy = IpAddressrGuavaFilter.getInstance();
        } else {
            strategy = IpAddressrGuavaFilter.getInstance();
        }
        return strategy;
    }
    public IAnalyzeFilter uaInstance(){
        if (properties.getFilterType() == null) {
            return null;
        }
        IAnalyzeFilter strategy;
        if (properties.getFilterType().equals(FilterTypeUtils.REDIS)) {
            strategy = applicationContext.getBean(UserAgentRedisFilter.class);
        } else if (properties.getFilterType().equals(FilterTypeUtils.GUAVA)) {
            strategy = UserAgentGuavaFilter.getInstance();
        } else {
            strategy = UserAgentGuavaFilter.getInstance();
        }
        return strategy;
    }
}
